<?php

class LocaleManager extends CApplicationComponent
{
    /**
     * Process PathInfo string of the requested URI
     * to set the application locale
     *
     * @param string $pathInfo
     * @return string
     */
    public function processPathInfo($pathInfo)
    {
        $localeSegment = substr($pathInfo, 0, 5);

        if (strlen($localeSegment) < 5) {
            // no locale info in the URI
            $this->setLanguage(Yii::app()->request->getPreferredLanguage());
        } else {
            $localeSegment = str_replace('/', '_', $localeSegment);
            if ($this->isLocaleIdValid($localeSegment)) {
                $pathInfo = substr($pathInfo, 6); // cut also the backslash after locale code
                // locale in URI is valid
                $this->setLanguage($localeSegment);
            } else {
                // locale in URI is not valid
                $this->setLanguage(Yii::app()->request->getPreferredLanguage());
            }
        }

        return $pathInfo;
    }

    public function languageCodeToUrlPrefix($language)
    {
        if ($this->isLocaleIdValid(strtolower($language))) {
            return str_replace('_', '/', strtolower($language));
        }
        return '';
    }


    protected function setLanguage($locale)
    {
        // locale could be a string that Browser submitted 
        // but FireFox sometimes submit only the language code, 
        // i.e. vi, en, not the fully qualified locale
        if ($this->isLocaleIdValid($locale)) {
            list($c, $l) = explode('_', $locale);
            Yii::app()->language = $c . '_' . strtoupper($l);
        } else {
            Yii::app()->language = null; // according to Yii, this maximum application performance
        }
    }

    public function isLocaleIdValid($locale)
    {
        $validLocaleCodes = array_keys($this->getStandardLocales());
        if (in_array($locale, $validLocaleCodes)) {
            return true;
        }
        return false;
    }

    public static function getStandardLocales()
    {
        $data = array(
            'aa_dj' => "Afar - Djibouti",
            'aa_er' => "Afar - Eritrea",
            'aa_et' => "Afar - Ethiopia",
            'af_na' => "Afrikaans - Namibia",
            'af_za' => "Afrikaans - South Africa",
            'ak_gh' => "Akan",
            'am_et' => "Amharic",
            'ar_ae' => "Arabic - United Arab Emirates",
            'ar_bh' => "Arabic - Bahrain",
            'ar_dz' => "Arabic - Algeria",
            'ar_eg' => "Arabic - Egypt",
            'ar_iq' => "Arabic - Iraq",
            'ar_jo' => "Arabic - Jordan",
            'ar_kw' => "Arabic - Kuwait",
            'ar_lb' => "Arabic - Lebanon",
            'ar_ly' => "Arabic - Libya",
            'ar_ma' => "Arabic - Morocco",
            'ar_om' => "Arabic - Oman",
            'ar_qa' => "Arabic - Qatar",
            'ar_sa' => "Arabic - Saudi Arabia",
            'ar_sd' => "Arabic - Sudan",
            'ar_sy' => "Arabic - Syria",
            'ar_tn' => "Arabic - Tunisia",
            'ar_ye' => "Arabic - Yemen",
            'as_in' => "Assamese",
            'az_az' => "Azerbaijani",
            'be_by' => "Belarusian",
            'bg_bg' => "Bulgarian",
            'bn_bd' => "Bengali - Bangladesh",
            'bn_in' => "Bengali - India",
            'bo_cn' => "Tibetan - China",
            'bo_in' => "Tibetan - India",
            'bs_ba' => "Bosnian",
            'byn_er' => "Blin",
            'ca_es' => "Catalan",
            'cch_ng' => "Atsam",
            'cs_cz' => "Czech",
            'cy_gb' => "Welsh",
            'da_dk' => "Danish",
            'de_at' => "German - Austria",
            'de_be' => "German - Belgium",
            'de_ch' => "German - Switzerland",
            'de_de' => "German - Germany",
            'de_li' => "German - Liechtenstein",
            'de_lu' => "German - Luxembourg",
            'dv_mv' => "Divehi",
            'dz_bt' => "Dzongkha",
            'ee_gh' => "Ewe - Ghana",
            'ee_tg' => "Ewe - Togo",
            'el_cy' => "Greek - Cyprus",
            'el_gr' => "Greek - Greece",
            'en_as' => "English - American Samoa",
            'en_au' => "English - Australia",
            'en_be' => "English - Belgium",
            'en_bw' => "English - Botswana",
            'en_bz' => "English - Belize",
            'en_ca' => "English - Canada",
            'en_gb' => "English - United Kingdom",
            'en_gu' => "English - Guam",
            'en_hk' => "English - Hong Kong SAR China",
            'en_ie' => "English - Ireland",
            'en_in' => "English - India",
            'en_jm' => "English - Jamaica",
            'en_mh' => "English - Marshall Islands",
            'en_mp' => "English - Northern Mariana Islands",
            'en_mt' => "English - Malta",
            'en_na' => "English - Namibia",
            'en_nz' => "English - New Zealand",
            'en_ph' => "English - Philippines",
            'en_pk' => "English - Pakistan",
            'en_sg' => "English - Singapore",
            'en_tt' => "English - Trinidad and Tobago",
            'en_um' => "English - U.S. Minor Outlying Islands",
            'en_us' => "English - United States",
            'en_vi' => "English - U.S. Virgin Islands",
            'en_za' => "English - South Africa",
            'en_zw' => "English - Zimbabwe",
            'es_ar' => "Spanish - Argentina",
            'es_bo' => "Spanish - Bolivia",
            'es_cl' => "Spanish - Chile",
            'es_co' => "Spanish - Colombia",
            'es_cr' => "Spanish - Costa Rica",
            'es_do' => "Spanish - Dominican Republic",
            'es_ec' => "Spanish - Ecuador",
            'es_es' => "Spanish - Spain",
            'es_gt' => "Spanish - Guatemala",
            'es_hn' => "Spanish - Honduras",
            'es_mx' => "Spanish - Mexico",
            'es_ni' => "Spanish - Nicaragua",
            'es_pa' => "Spanish - Panama",
            'es_pe' => "Spanish - Peru",
            'es_pr' => "Spanish - Puerto Rico",
            'es_py' => "Spanish - Paraguay",
            'es_sv' => "Spanish - El Salvador",
            'es_us' => "Spanish - United States",
            'es_uy' => "Spanish - Uruguay",
            'es_ve' => "Spanish - Venezuela",
            'et_ee' => "Estonian",
            'eu_es' => "Basque",
            'fa_af' => "Persian - Afghanistan",
            'fa_ir' => "Persian - Iran",
            'fi_fi' => "Finnish",
            'fil_ph' => "Filipino",
            'fo_fo' => "Faroese",
            'fr_be' => "French - Belgium",
            'fr_ca' => "French - Canada",
            'fr_ch' => "French - Switzerland",
            'fr_fr' => "French - France",
            'fr_lu' => "French - Luxembourg",
            'fr_mc' => "French - Monaco",
            'fr_sn' => "French - Senegal",
            'fur_it' => "Friulian",
            'ga_ie' => "Irish",
            'gaa_gh' => "Ga",
            'gez_er' => "Geez - Eritrea",
            'gez_et' => "Geez - Ethiopia",
            'gl_es' => "Galician",
            'gsw_ch' => "Swiss - German",
            'gu_in' => "Gujarati",
            'gv_gb' => "Manx",
            'ha_gh' => "Hausa - Ghana",
            'ha_ne' => "Hausa - Niger",
            'ha_ng' => "Hausa - Nigeria",
            'ha_sd' => "Hausa - Sudan",
            'haw_us' => "Hawaiian",
            'he_il' => "Hebrew",
            'hi_in' => "Hindi",
            'hr_hr' => "Croatian",
            'hu_hu' => "Hungarian",
            'hy_am' => "Armenian",
            'id_id' => "Indonesian",
            'ig_ng' => "Igbo",
            'ii_cn' => "Sichuan Yi",
            'is_is' => "Icelandic",
            'it_ch' => "Italian",
            'it_it' => "Italian Italy",
            'ja_jp' => "Japanese",
            'ka_ge' => "Georgian",
            'kaj_ng' => "Jju",
            'kam_ke' => "Kamba",
            'kcg_ng' => "Tyap",
            'kfo_ci' => "Koro",
            'kk_kz' => "Kazakh",
            'kl_gl' => "Kalaallisut",
            'km_kh' => "Khmer",
            'kn_in' => "Kannada",
            'ko_kr' => "Korean",
            'kok_in' => "Konkani",
            'kpe_gn' => "Kpelle - Guinea",
            'kpe_lr' => "Kpelle - Liberia",
            'ku_iq' => "Kurdish - Iraq",
            'ku_ir' => "Kurdish - Iran",
            'ku_sy' => "Kurdish - Syria",
            'ku_tr' => "Kurdish - Turkey",
            'kw_gb' => "Cornish",
            'ky_kg' => "Kirghiz",
            'ln_cd' => "Lingala - Congo - Kinshasa",
            'ln_cg' => "Lingala - Congo - Brazzaville",
            'lo_la' => "Lao",
            'lt_lt' => "Lithuanian",
            'lv_lv' => "Latvian",
            'mk_mk' => "Macedonian",
            'ml_in' => "Malayalam",
            'mn_cn' => "Mongolian - China",
            'mn_mn' => "Mongolian - Mongolia",
            'mr_in' => "Marathi",
            'ms_bn' => "Malay - Brunei",
            'ms_my' => "Malay - Malaysia",
            'mt_mt' => "Maltese",
            'my_mm' => "Burmese",
            'nb_no' => "Norwegian Bokmål",
            'nds_de' => "Low German",
            'ne_in' => "Nepali",
            'ne_np' => "Nepali Nepal",
            'nl_be' => "Dutch - Belgium",
            'nl_nl' => "Dutch - Netherlands",
            'nn_no' => "Norwegian Nynorsk",
            'nr_za' => "South Ndebele",
            'nso_za' => "Northern Sotho",
            'ny_mw' => "Nyanja",
            'oc_fr' => "Occitan",
            'om_et' => "Oromo - Ethiopia",
            'om_ke' => "Oromo - Kenya",
            'or_in' => "Oriya",
            'pa_in' => "Punjabi",
            'pa_pk' => "Punjabi - Pakistan",
            'pl_pl' => "Polish",
            'ps_af' => "Afghanistan",
            'pt_br' => "Portuguese - Brazil",
            'pt_pt' => "Portuguese - Portugal",
            'ro_md' => "Romanian - Moldova",
            'ro_ro' => "Romanian - Romania",
            'ru_ru' => "Russian - Russia",
            'ru_ua' => "Russian - Ukraine",
            'rw_rw' => "Kinyarwanda",
            'sa_in' => "Sanskrit",
            'se_fi' => "Northern Sami Finland",
            'se_no' => "Northern Sami Norway",
            'sh_ba' => "Serbo - Croatian Bosnia and Herzegovina",
            'sh_cs' => "Serbo - Croatian Serbia and Montenegro",
            'si_lk' => "Sinhala",
            'sid_et' => "Sidamo",
            'sk_sk' => "Slovak",
            'sl_si' => "Slovenian",
            'so_dj' => "Somali - Djibouti",
            'so_et' => "Somali - Ethiopia",
            'so_ke' => "Somali - Kenya",
            'so_so' => "Somali - Somalia",
            'sq_al' => "Albanian",
            'sr_ba' => "Serbian - Bosnia and Herzegovina",
            'sr_cs' => "Serbian - Serbia and Montenegro",
            'sr_me' => "Serbian - Montenegro",
            'sr_rs' => "Serbian - Serbia",
            'ss_sz' => "Swati - Swaziland",
            'ss_za' => "Swati - South Africa",
            'st_ls' => "Southern Sotho Lesotho",
            'st_za' => "Southern Sotho South Africa",
            'sv_fi' => "Swedish - Finland",
            'sv_se' => "Swedish - Sweden",
            'sw_ke' => "Swahili - Kenya",
            'sw_tz' => "Swahili - Tanzania",
            'syr_sy' => "Syriac",
            'ta_in' => "Tamil",
            'te_in' => "Telugu",
            'tg_tj' => "Tajik",
            'th_th' => "Thai",
            'ti_er' => "Tigrinya - Eritrea",
            'ti_et' => "Tigrinya - Ethiopia",
            'tig_er' => "Tigre",
            'tn_za' => "Tswana",
            'to_to' => "Tonga",
            'tr_tr' => "Turkish",
            'trv_tw' => "Taroko",
            'ts_za' => "Tsonga",
            'tt_ru' => "Tatar",
            'ug_cn' => "Uighur",
            'uk_ua' => "Ukrainian",
            'ur_in' => "Urdu - India",
            'ur_pk' => "Urdu - Pakistan",
            'uz_af' => "Uzbek - Afghanistan",
            'uz_uz' => "Uzbek - Uzbekistan",
            've_za' => "Venda",
            'vi_vn' => "Vietnamese",
            'wal_et' => "Walamo",
            'wo_sn' => "Wolof",
            'xh_za' => "Xhosa",
            'yo_ng' => "Yoruba",
            'zh_cn' => "Chinese - China",
            'zh_hk' => "Chinese - Hong Kong SAR China",
            'zh_mo' => "Chinese - Macau SAR China",
            'zh_sg' => "Chinese - Singapore",
            'zh_tw' => "Chinese - Taiwan",
            'zu_za' => "Zulu",
        );
        asort($data);
        return $data;
    }
}

?>
